/*
@Time : 2021/2/7 10:41
@Author : 吴周海
@Mail: wzh200x@126.com
@File : MySqlHelper.go
@Software: GoLand
*/
package DBHelper

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/config"
	_ "github.com/go-sql-driver/mysql"
)

func MySqlInit() {
	orm.RegisterDriver("mysql", orm.DRMySQL)
	SetUpDefaultMysqlDB()
}

func SetUpDefaultMysqlDB() {
	mysqlHost, _ := config.String("mysqlHost")
	mysqlPort, _ := config.String("mysqlPort")
	mysqlUser, _ := config.String("mysqlUser")
	mysqlPass, _ := config.String("mysqlPass")
	mysqlDBName, _ := config.String("mysqlDBName")

	connectStr := "" + mysqlUser + ":" + mysqlPass + "@tcp(" + mysqlHost + ":" + mysqlPort + ")/" + mysqlDBName +
		"?charset=utf8&loc=Local"
	// 注册数据库
	orm.RegisterDataBase("default", "mysql", connectStr, orm.MaxIdleConnections(30), orm.MaxOpenConnections(30))
	// 同步数据库
	orm.RunSyncdb("default", false, true)
}
